package com.mediacloud.app.newsmodule.view

import android.content.Context
import android.graphics.Bitmap
import androidx.fragment.app.FragmentActivity
import com.mediacloud.app.assembly.util.FileUtil
import com.mediacloud.app.assembly.util.Utility
import com.mediacloud.app.assembly.util.toastCenter
import com.mediacloud.app.model.interfaces.IPermissionsAgree
import com.mediacloud.app.model.interfaces.PermissionUtil
import com.mediacloud.app.model.utils.ViewUtils
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.utils.ToastUtil
import com.mediacloud.app.share.SocialShareControl
import com.mediacloud.app.share.view.SharePopGridWindow
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import java.io.File
import java.util.HashMap

//直接分享图片 不显示出来。
class PosterShareGridPopX(context: Context) : SharePopGridWindow(context) {


    override fun getResLayoutId(): Int {
        return R.layout.popwin_share_image
    }


    fun saveImage(context: Context, bitmap: Bitmap?) {
        PermissionUtil.invokeStoragePermission(context as FragmentActivity, object : IPermissionsAgree {
            override fun onAgree() {
                save(bitmap)
            }

            override fun onRefuse() {
                dismiss()
            }

            override fun hasPermissions() {
                save(bitmap)
            }
        })

    }

    private fun save(bitmap: Bitmap?) {
        doAsync {
            val path = FileUtil.getPhotoApp() + "/" + System.currentTimeMillis() + ".jpg"
            FileUtil.saveBitmap(path, bitmap)
            uiThread {
                val file = File(path)
                Utility.insertIntoMediaStore(contentView.context, false, file, file.lastModified())
                toastCenter(contentView.context, "图片保存成功")
                dismiss()
                bitmap?.recycle()
            }
        }
    }

    fun shareImage(context: Context, bitmap: Bitmap?, share_media: Int) {
        shareImage(context, bitmap, share_media, object : SocialShareControl.SocialShareListener {

            override fun shareComplete(p0: Int, p1: HashMap<String, Any>?) {
                dismiss()
                SocialShareControl.removeSocialShareListener(this)
            }

            override fun shareCancel(p0: Int) {
                dismiss()
                SocialShareControl.removeSocialShareListener(this)
            }

            override fun shareError(p0: Int, p1: String?) {
                dismiss()
                SocialShareControl.removeSocialShareListener(this)
            }
        })
    }

    fun shareImage(context: Context, bitmap: Bitmap?, share_media: Int, listener: SocialShareControl.SocialShareListener? = null) {
        if (bitmap == null) {
            SocialShareControl.shareError(share_media, "分享图片不能为空")
            return
        }
        PermissionUtil.invokeStoragePermission(context as FragmentActivity, object : IPermissionsAgree{
            override fun onAgree() {
                realShare(bitmap, context, share_media, listener)
            }

            override fun onRefuse() {
            }

            override fun hasPermissions() {
                realShare(bitmap, context, share_media, listener)
            }
        })
    }

    private fun realShare(bitmap: Bitmap?, context: Context, share_media: Int, listener: SocialShareControl.SocialShareListener?) {
        doAsync {
            val path = FileUtil.saveShareBitmap(bitmap)
            uiThread {
                SocialShareControl.share(ViewUtils.searchTintContextHostActivity(context), share_media, path)
                SocialShareControl.addSocialShareListener(listener)
            }
        }
    }
}